package controllers;

import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import models.Operator;
import models.Oplog;
import models.Operator;
import models.Operator;
import models.User;

import org.apache.commons.lang.StringUtils;

import play.libs.Codec;
import services.AuthorityService;
import services.OperatorService;
import services.OperatorService;
import services.OperatorService;
import services.UserInfoService;
import utils.BizConstants;
import utils.Validate;
import vos.OperatorSearchVo;
import vos.ReturnCode;
import vos.ReturnListVo;
import vos.OperatorSearchVo;

public class OperatorController extends BaseController {
	/**
	 * 修改密码页面
	 *
	 * @Title: editPassword 
	 * @return: void
	 */
	public static void editPassword(String menuid){
		Operator oper=getLoginUser();
		List<String> auths=AuthorityService.getAuthorityByOperAndMenu(menuid,oper.operatorid);
		for(String auth : auths){
			renderArgs.put(auth, auth);
		}
		render();
	}
	/**
	 * 重置密码
	 * @Param idcard 身份证号
	 * @Title: resetPass 
	 * @return: void
	 */
	public static void resetPass(String operatorid){
		if(!checkAuthority("OPERATOR_RESET")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		if(StringUtils.isEmpty(operatorid)){
			renderFailedJson(ReturnCode.ErrorCode, "身份证号不能为空");
		}
		Operator oper=OperatorService.findById(operatorid);
		if(oper==null){
			renderFailedJson(ReturnCode.ErrorCode, "用户不存在");
		}
		boolean isOk=OperatorService.resetPass(oper);
		if(isOk){
			Oplog oplog=new Oplog(oper.operatorid,"重置操作员密码："+operatorid,"操作员管理");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode, "重置密码成功");
		}
		else{
			renderFailedJson(ReturnCode.SuccessCode, "重置密码失败");
		}
		
	}
	/**
	 * 列表页面
	 *
	 * @Title: editPassword 
	 * @return: void
	 */
	public static void operList(String menuid){
		Operator oper=getLoginUser();
		List<String> auths=AuthorityService.getAuthorityByOperAndMenu(menuid,oper.operatorid);
		for(String auth : auths){
			renderArgs.put(auth, auth);
		}
		render();
	}
	/**
	 * 获取操作员列表
	 *
	 * @Title: getOperator 
	 * @return: void
	 */
	public static void getOperator(int page,int rows, String sort,String order,String operatorname,String operatorid,Boolean isflag){
		if(page==0){
			page=1;
		}
		if(rows==0){
			rows=BizConstants.pagesize;
		}
		OperatorSearchVo vo =new OperatorSearchVo(page,rows,order,sort,operatorname,operatorid,isflag);
		long total=OperatorService.countOperator(vo);
		ReturnListVo listVo=new ReturnListVo();
		listVo.setTotal(total);
		List<Operator> lists=OperatorService.listOperator(vo);
		listVo.setRows(lists);
		renderJSON(listVo);
	}
	/**
	 * 批量启用
	 *
	 * @Title: betchEnable 
	 * @return: void
	 */
	public static void betchEnable(String ids){
		if(!checkAuthority("OPERATOR_ENABLE")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		Operator oper=getLoginUser();
		if(StringUtils.isEmpty(ids)){
			renderFailedJson(ReturnCode.ErrorCode,"未选中记录");
		}
		boolean issuccess=OperatorService.betchEnable(ids,oper.operatorid);
		if(issuccess){
			Oplog oplog=new Oplog(oper.operatorid,"批量启用操作员："+ids,"操作员管理");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode,"启用成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode,"启用失败");
		}
	}
	/**
	 * 批量停用
	 *
	 * @Title: betchEnable 
	 * @return: void
	 */
	public static void betchDisable(String ids){
		if(!checkAuthority("OPERATOR_DISABLE")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		Operator oper=getLoginUser();
		if(StringUtils.isEmpty(ids)){
			renderFailedJson(ReturnCode.ErrorCode,"未选中记录");
		}
		boolean issuccess=OperatorService.betchDisable(ids,oper.operatorid);
		if(issuccess){
			Oplog oplog=new Oplog(oper.operatorid,"批量停用："+ids,"操作员管理");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode,"停用成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode,"停用失败");
		}
	}
	/**
	 * 批量删除
	 *
	 * @Title: betchEnable 
	 * @return: void
	 */
	public static void betchDel(String ids){
		if(!checkAuthority("OPERATOR_BETCHDEL")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		if(StringUtils.isEmpty(ids)){
			renderFailedJson(ReturnCode.ErrorCode,"未选中记录");
		}
		boolean issuccess=OperatorService.betchDel(ids);
		if(issuccess){
			Operator oper=getLoginUser();
			Oplog oplog=new Oplog(oper.operatorid,"批量删除："+ids,"操作员管理");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode,"删除成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode,"删除失败");
		}
	}
	/**
	 * 删除记录
	 *
	 * @Title: delOperator 
	 * @param id 操作员id
	 * @return: void
	 */
	public static void delOperator(String id){
		if(!checkAuthority("OPERATOR_DELETE")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		if(StringUtils.isEmpty(id)){
			renderFailedJson(ReturnCode.ErrorCode,"选中记录不能为空");
		}
		boolean issuccess=OperatorService.delOperator(id);
		if(issuccess){
			Operator oper=getLoginUser();
			Oplog oplog=new Oplog(oper.operatorid,"删除操作员："+id,"操作员管理");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode,"删除成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode,"删除失败");
		}
	}
	/**
	 * 添加操作员
	 *
	 * @Title: addOperator 
	 * @return: void
	 */
	public static void operatorAdd(){
		render();
	}
	/**
	 * 复制操作员
	 *
	 * @Title: addOperator 
	 * @return: void
	 */
	public static void operatorCopy(String id){
		Operator oldOperator=OperatorService.getById(id);
		renderArgs.put("operator", oldOperator);
		render("OperatorController/operatorAdd.html");
	}
	/**
	 * 验证操作员编号是否存在
	 *
	 * @Title: checkCode 
	 * @param operatorid 操作员id
	 * @return: void
	 */
	public static void checkCode(String value){
//		if(!Validate.isEnglishNum(value)){
//			renderFailedJson(ReturnCode.ErrorCode, "操作员编号格式不正确");
//		}
		if(StringUtils.isEmpty(value)){
			renderFailedJson(ReturnCode.ErrorCode, "编号不能为空");
		}
		boolean isexist=OperatorService.checkCode(value);
		if(isexist){
			renderFailedJson(ReturnCode.ErrorCode, "编号已存在");
		}
		else{
			renderFailedJson(ReturnCode.SuccessCode, "该编号可以使用");
		}
	}
	/**
	 * 保存操作员信息
	 *
	 * @Title: saveOperator 
	 * @param operator 操作员对象
	 * @return: void
	 */
	public static void saveOperator(Operator operator){
		if(!checkAuthority("OPERATOR_ADD")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		Operator oper=getLoginUser();
		if(operator==null){
			renderFailedJson(ReturnCode.ErrorCode, "操作员不能为空");
		}
		if(StringUtils.isEmpty(operator.operatorid)){
			renderFailedJson(ReturnCode.ErrorCode, "编号不能为空");
		}
		Operator oldOperator=OperatorService.getById(operator.operatorid);
		if(oldOperator!=null){
			renderFailedJson(ReturnCode.ErrorCode, "操作员已存在");
		}
		//验证是不是数字和英语字母组成
//		if(!Validate.isEnglishNum(operator.operatorid)){
//			renderFailedJson(ReturnCode.ErrorCode, "操作员编号格式不正确");
//		}
		//验证手机号
		if(!Validate.isMobile(operator.operatormobile)){
			renderFailedJson(ReturnCode.ErrorCode, "手机格式不正确");
		}
		if(!Validate.isEmail(operator.email)){
			renderFailedJson(ReturnCode.ErrorCode, "邮箱不正确");
		}
		operator.createname=oper.operatorid;
		boolean isSuccess=OperatorService.saveOperator(operator);
		if(isSuccess){
			Oplog oplog=new Oplog(oper.operatorid,"添加操作员："+operator.operatorid,"操作员管理");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode, "保存成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode, "保存失败");
		}
	}
	/**
	 * 编辑操作员
	 *
	 * @Title: operatorEdit 
	 * @return: void
	 */
	public static void operatorEdit(String id){
		Operator oldOperator=OperatorService.getById(id);
		renderArgs.put("operator", oldOperator);
		render();
	}
	/**
	 * 保存操作员信息
	 *
	 * @Title: saveOperator 
	 * @param operator 操作员对象
	 * @return: void
	 */
	public static void updateOperator(Operator operator){
		if(!checkAuthority("OPERATOR_EDIT")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		Operator oper=getLoginUser();
		if(operator==null){
			renderFailedJson(ReturnCode.ErrorCode, "操作员不能为空");
		}
		if(StringUtils.isEmpty(operator.operatorid)){
			renderFailedJson(ReturnCode.ErrorCode, "编号不能为空");
		}
		Operator oldOperator=OperatorService.getById(operator.operatorid);
		if(oldOperator==null){
			renderFailedJson(ReturnCode.ErrorCode, "操作员不存在");
		}
		operator.modifyname=oper.operatorid;
		boolean isSuccess=OperatorService.updateOperator(operator);
		if(isSuccess){
			Oplog oplog=new Oplog(oper.operatorid,"编辑操作员："+operator.operatorid,"操作员管理");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode, "保存成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode, "保存失败");
		}
	}
}
